﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Windows.Forms;
using VietKinsNutrition.Bussiness;
using VietKinsNutrition.Common;
using VietKinsNutrition.MoRong;

namespace VietKinsNutrition.UserControl
{
    public partial class FrmThucdontuan : DevExpress.XtraEditors.XtraUserControl
    {
        ClsTblThamsodauvaoBLL ClsTblThamsodauvaobll = new ClsTblThamsodauvaoBLL();
        ClsThucdon_monanBLL ClsThucdon_monanbll = new ClsThucdon_monanBLL();
        ClsTblDm_BuaantrongngayBLL ClsTblDm_Buaantrongngaybll = new ClsTblDm_BuaantrongngayBLL();
        public FrmThucdontuan()
        {
            InitializeComponent();
            datNgay.EditValue = DateTime.Now;
            ClsThucdon_monanbll.Bind_Khoilop(cboKhoi);
            UserComboBoxItem.SelectItem(cboKhoi, "1");
            try
            {
            }
            catch { }
            lbNgay.Text = "" + Returnthu(datNgay.DateTime) + "";
        }

        private void FrmThucdontuan_Load(object sender, EventArgs e)
        {
            cboKhoi_EditValueChanged(sender, e);
        }

        private DateTime GetDates(DateTime dDateTime, string Var)
        {
            System.DateTime stDate; System.DateTime endDate;
            int trunggian = 0;
            double offset = 0;
            switch (dDateTime.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    offset = 0;
                    trunggian = 5;
                    break;
                case DayOfWeek.Tuesday:
                    offset = -1;
                    trunggian = 4;
                    break;
                case DayOfWeek.Wednesday:
                    offset = -2;
                    trunggian = 3;
                    break;
                case DayOfWeek.Thursday:
                    offset = -3;
                    trunggian = 2;
                    break;
                case DayOfWeek.Friday:
                    offset = -4;
                    trunggian = 1;
                    break;
                case DayOfWeek.Saturday:
                    offset = -5;
                    trunggian = 0;
                    break;
                case DayOfWeek.Sunday:
                    offset = -6;
                    trunggian = 0;
                    break;
            }

            stDate = dDateTime.AddDays(offset);
            endDate = dDateTime.AddDays(5 + offset);

            if (Var == "0")
                return stDate;
            else
                return endDate;
        }

        private string Retunrn_sThu(DateTime Value, int iKhoilopId, int BuaantrongngayId)
        {
            string sReturn = "";
            DataGridView Grid = new DataGridView();
            ClsThucdon_monanbll.Bind_GridSeletByNgay(Value, Grid);
            DataTable Tbl = new DataTable();
            Tbl = (DataTable)Grid.DataSource;
            foreach (DataRow Row in Tbl.Rows)
            {
                if (iKhoilopId.ToString().Trim() == Row["KhoilopId"].ToString().Trim() && BuaantrongngayId.ToString().Trim() == Row["BuaantrongngayId"].ToString().Trim())
                {

                    sReturn = Row["Tenmonan"].ToString();
                }
            }
            return sReturn;
        }

        static DataTable newTbl;
        static DataTable Tbl;
        DataGridView NewGrid;
        private DataTable RetunrnThucdontuan(int iKhoilopId, DateTime Value)
        {
            try
            {
                newTbl = new DataTable();
                Tbl = new DataTable();
                NewGrid = new DataGridView();
                ClsTblDm_Buaantrongngaybll.BingSource_DataGrid(NewGrid);
                newTbl = ((DataSet)((BindingSource)NewGrid.DataSource).DataSource).Tables[0];
                Tbl.Columns.Add("BuaantrongngayId", typeof(int));
                Tbl.Columns.Add("Tenbuaan", typeof(string));
                Tbl.Columns.Add("Thu2", typeof(string));
                Tbl.Columns.Add("Thu3", typeof(string));
                Tbl.Columns.Add("Thu4", typeof(string));
                Tbl.Columns.Add("Thu5", typeof(string));
                Tbl.Columns.Add("Thu6", typeof(string));
                Tbl.Columns.Add("Thu7", typeof(string));
                //

                advBandedGridView1.Columns["Thu2"].Caption = "Thứ 2\n" + GetDates(Value, "0").Day.ToString() + "/" + GetDates(Value, "0").Month.ToString() + "/" + GetDates(Value, "0").Year.ToString();
                advBandedGridView1.Columns["Thu3"].Caption = "Thứ 3\n" + GetDates(Value, "0").AddDays(1).Day.ToString() + "/" + GetDates(Value, "0").AddDays(1).Month.ToString() + "/" + GetDates(Value, "0").AddDays(1).Year.ToString();
                advBandedGridView1.Columns["Thu4"].Caption = "Thứ 4\n" + GetDates(Value, "0").AddDays(2).Day.ToString() + "/" + GetDates(Value, "0").AddDays(2).Month.ToString() + "/" + GetDates(Value, "0").AddDays(2).Year.ToString();
                advBandedGridView1.Columns["Thu5"].Caption = "Thứ 5\n" + GetDates(Value, "0").AddDays(3).Day.ToString() + "/" + GetDates(Value, "0").AddDays(3).Month.ToString() + "/" + GetDates(Value, "0").AddDays(3).Year.ToString();
                advBandedGridView1.Columns["Thu6"].Caption = "Thứ 6\n" + GetDates(Value, "0").AddDays(4).Day.ToString() + "/" + GetDates(Value, "0").AddDays(4).Month.ToString() + "/" + GetDates(Value, "0").AddDays(4).Year.ToString();
                advBandedGridView1.Columns["Thu7"].Caption = "Thứ 7\n" + GetDates(Value, "0").AddDays(5).Day.ToString() + "/" + GetDates(Value, "0").AddDays(5).Month.ToString() + "/" + GetDates(Value, "0").AddDays(5).Year.ToString();
                //
                foreach (DataRow Row in newTbl.Rows)
                {
                    Tbl.Rows.Add(Row["Id"], Row["Tenbuaan"],
                         Retunrn_sThu(GetDates(Value, "0"), iKhoilopId, int.Parse(Row["Id"].ToString()))
                        , Retunrn_sThu(GetDates(Value, "0").AddDays(1), iKhoilopId, int.Parse(Row["Id"].ToString())),
                         Retunrn_sThu(GetDates(Value, "0").AddDays(2), iKhoilopId, int.Parse(Row["Id"].ToString())),
                         Retunrn_sThu(GetDates(Value, "0").AddDays(3), iKhoilopId, int.Parse(Row["Id"].ToString())),
                         Retunrn_sThu(GetDates(Value, "0").AddDays(4), iKhoilopId, int.Parse(Row["Id"].ToString())),
                         Retunrn_sThu(GetDates(Value, "0").AddDays(5), iKhoilopId, int.Parse(Row["Id"].ToString()))
                        );
                }
                return Tbl;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private string Returnthu(DateTime dDateTime)
        {
            string thu = "";
            switch (dDateTime.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    thu = "Thứ 2";
                    break;
                case DayOfWeek.Tuesday:
                    thu = "Thứ 3";
                    break;
                case DayOfWeek.Wednesday:
                    thu = "Thứ 4";
                    break;
                case DayOfWeek.Thursday:
                    thu = "Thứ 5";
                    break;
                case DayOfWeek.Friday:
                    thu = "Thứ 6";
                    break;
                case DayOfWeek.Saturday:
                    thu = "Thứ 7";
                    break;
                case DayOfWeek.Sunday:
                    thu = "Chủ nhật";
                    break;
            }
            return thu;

        }

        private void cboKhoi_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.ClsThucdon_monanbll.BingSource_DataGridThucdontuan(advBandedGridView1, gridControl1, Convert.ToInt32(UserComboBoxItem.GetItemValue(cboKhoi)), GetDates(datNgay.DateTime, "0"));
                advBandedGridView1.Bands[1].Caption = "Khối " + cboKhoi.Text;
            }
            catch { }
        }

        private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            try
            {
                string[] Tg = advBandedGridView1.Columns[e.Column.FieldName].Caption.Split(',');
                string[] Tg2 = Tg[1].Split('-');
                string tt, nn;
                if (int.Parse(Tg2[1].ToString()) < 10)
                {
                    tt = "0" + Tg2[1].ToString().Trim();
                }
                else
                { tt = Tg2[1].ToString(); }

                if (int.Parse(Tg2[0].ToString()) < 10)
                {
                    nn = "0" + Tg2[0].ToString().Trim();
                }
                else
                { nn = Tg2[0].ToString(); }
                DateTime _dNgay = DateTime.Parse(nn + "/" + tt + "/" + Tg2[2].ToString());
                int iBuaantrongngayId = int.Parse(advBandedGridView1.GetFocusedRowCellValue("BuaantrongngayId").ToString());
                // kiểm tra nếu chưa có thì thêm mới
                string Giatri = advBandedGridView1.GetFocusedRowCellValue(e.Column.FieldName).ToString().Replace("\r", " ");

                if (ClsThucdon_monanbll.TblThucdon_monanByVar(_dNgay, iBuaantrongngayId, Convert.ToInt32(UserComboBoxItem.GetItemValue(cboKhoi))).Rows.Count <= 0)
                {
                    if (advBandedGridView1.GetFocusedRowCellValue(e.Column.FieldName).ToString().Trim() != "")
                    {
                        ClsThucdon_monanbll.Add(_dNgay, iBuaantrongngayId, Convert.ToInt32(UserComboBoxItem.GetItemValue(cboKhoi)), Giatri);
                    }
                }
                else
                {
                    // sửa ở đây
                    if (advBandedGridView1.GetFocusedRowCellValue(e.Column.FieldName).ToString().Trim() != "")
                    {
                        // xóa trước
                        ClsThucdon_monanbll.TblThucdon_monan_Del_ByNgay_KhoilopId_BuaantrongngayId(_dNgay, iBuaantrongngayId, Convert.ToInt32(UserComboBoxItem.GetItemValue(cboKhoi)));
                        // thêm mới
                        ClsThucdon_monanbll.Add(_dNgay, iBuaantrongngayId, Convert.ToInt32(UserComboBoxItem.GetItemValue(cboKhoi)), Giatri);
                    }
                    else
                    {
                        // là xóa
                        ClsThucdon_monanbll.TblThucdon_monan_Del_ByNgay_KhoilopId_BuaantrongngayId(_dNgay, iBuaantrongngayId, Convert.ToInt32(UserComboBoxItem.GetItemValue(cboKhoi)));
                    }
                }
            }
            catch { }
        }

        private void datNgay_EditValueChanged(object sender, EventArgs e)
        {
            cboKhoi_EditValueChanged(sender, e);
        }

        private void btnLaytruoc_Click(object sender, EventArgs e)
        {
            FrmChonthucdontuan frmChonthucdontuan = new FrmChonthucdontuan();
            frmChonthucdontuan.ShowDialog();
            cboKhoi_EditValueChanged(sender, e);
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            try
            {
                string[] Tg = advBandedGridView1.Columns["Thu2"].Caption.Split(',');
                string[] Tg2 = Tg[1].Split('-');
                string tt, nn;
                if (int.Parse(Tg2[1].ToString()) < 10)
                {
                    tt = "0" + Tg2[1].ToString().Trim();
                }
                else
                { tt = Tg2[1].ToString(); }

                if (int.Parse(Tg2[0].ToString()) < 10)
                {
                    nn = "0" + Tg2[0].ToString().Trim();
                }
                else
                { nn = Tg2[0].ToString(); }
                DateTime _dNgay = DateTime.Parse(nn + "/" + tt + "/" + Tg2[2].ToString());
                //       ClsExel.ExportThucdontrongtuan(_dNgay);
                if (ClsTblThamsodauvaobll.SelectAll().Rows.Count > 0 && ClsTblThamsodauvaobll.SelectAll().Rows[0]["Baoanchitiet"].ToString() != "True")

                    ClsExel.ExportThucdontrongtuan_truong(_dNgay, int.Parse(UserComboBoxItem.GetItemValue(cboKhoi)), cboKhoi.Text);
                else
                    ClsExel.ExportThucdontrongtuan(_dNgay);
            }
            catch { }
        }
    }
}
